<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AuthController
 *
 * @author Brian Voss
 */
class AuthController extends Zend_Rest_Controller {

    protected $user;

    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
    {
	parent::__construct($request, $response, $invokeArgs);
    }

    public function init()
    {
	$this->user = new Application_Model_User();
	//$this->_helper->layout->disableLayout();

	// Filter and Validate Expected GET Input
	$filters = array(
	    '*' => array(
		'StringTrim'
	    )
	);
	$validators = array(
	    'id'    => array(),
	    'email' => array(),
	    'password' => array()
	);
	$this->params = new Zend_Filter_Input($filters, $validators, $this->_request->getParams());
	$this->_helper->viewRenderer->setNoRender();
    }

    public function indexAction()
    {
	$response = array();
	$this->getResponse()
	    ->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')
	    ->appendBody(json_encode($response));
    }

    public function getAction()
    {
	if ( !empty($this->params->id) )
	{
	    switch( $this->params->id)
	    {
		case "login":
		    //$this->_helper->viewRenderer->setRender('login');
		    $this->render('login');
		    break;		    
	    }
	}
	else
	{
	    $response = array();
	    $this->getResponse()
		->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')
		->appendBody(json_encode($response));
	}
    }

    public function postAction()
    {
	if ( $this->params->isValid() )
	{
	    $email = $this->params->email;
	    $password = $this->params->password;
	    $user = null;
	    //==
	    $result = $this->user->authenticate($email, $password);
	    if ( !empty($result))
	    {
		$user = $result->toArray();
		//nyah
	    }
	}
	else
	{
	    $user = false;
	}


	$this->getResponse()
	    ->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')
	    ->appendBody(json_encode($user));
    }

    public function putAction()
    {
	$response = array();
	$this->getResponse()
	    ->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')
	    ->appendBody(json_encode($response));
    }

    public function deleteAction()
    {
	$response = array();
	$this->getResponse()
	    ->setHeader(Zend_Http_Client::CONTENT_TYPE, 'application/json')
	    ->appendBody(json_encode($response));
    }


}
?>
